package mechanism.performance;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Array {
	
	private static String[] s = new String[5000000];
	
	private static List<String> lst;
	
	static {
		lst = new ArrayList<String>();
		for(int i=0; i<s.length; i++) {
			s[i] = "test";
			lst.add(s[i]);
		}
	}
	
	public static void main(String[] args) {
		
		String[] methodNames = {"testArray", "testArrayAsList", "testArrayList"};
		for(String s : methodNames) {
			testFunc(s);
		}
	}
	
	public static void testArray() {
		String str;
		for(int i=0; i<s.length; i++) {
			str = s[i];
		}
	}
	
	public static void testArrayAsList() {
		List<String> strView = Arrays.asList(s);
		String str;
		for(int i=0; i<s.length; i++) {
			str = strView.get(i);
		}
	}
	
	public static void testArrayList() {
		String str;
		for(int i=0; i<lst.size(); i++) {
			str = lst.get(i);
		}
	}
	
	public static void testFunc(String funcName) {
		try {
			Method m = Array.class.getMethod(funcName, new Class[0]);
			long start = System.currentTimeMillis();
			m.invoke(null, new Object[0]);
			long end = System.currentTimeMillis();
			System.out.println(m.getName() + ": " + (end - start));
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
